<template>
  <div class="register-container">
    <el-card class="register-card">
      <div class="logo">
        <img src="@/assets/logo.png" alt="Logo">
      </div>
      <h2>简历分析系统注册</h2>
      <el-form :model="form" :rules="rules" ref="registerForm">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="用户名" size="large"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="密码" size="large"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" placeholder="确认密码" size="large"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleRegister" size="large">注册</el-button>
          <el-button @click="$router.push('/login')" size="large">返回登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const form = ref({
  username: '',
  password: '',
  confirmPassword: ''
})

const validatePassword = (rule, value, callback) => {
  if (value !== form.value.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validatePassword, trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  try {
    await userStore.register({
      username: form.value.username,
      password: form.value.password
    })
    router.push('/user')
  } catch (error) {
    console.error(error)
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('@/assets/big1.jpg') no-repeat center center;
  background-size: cover;
  position: relative;
}
.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
}
.register-card {
  width: 100%;
  max-width: 500px;
  padding: 40px;
  z-index: 1;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  margin: 0 auto;
  border: 1px solid rgba(0, 0, 0, 0.05);
}
.logo {
  text-align: center;
  margin-bottom: 30px;
}
.logo img {
  height: 120px;
}
h2 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 40px;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 1px;
}
.el-button {
  padding: 12px 28px;
  font-size: 16px;
  border-radius: 6px;
  transition: all 0.3s ease;
}
.el-button--primary {
  background: #409eff;
  border-color: #409eff;
}
.el-button--primary:hover {
  background: #66b1ff;
  border-color: #66b1ff;
}
.el-input {
  font-size: 16px;
}
</style>
